package taskTracker.ui.lwuit;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;

import taskTracker.Project;

import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;

/**
 * A form that lists some Projects and allow navigation between them
 * 
 * @author aseldawy
 * 
 */
public class ProjectListUI extends Form implements ActionListener {

	/** The list that holds all task lists stored */
	private List lstProjects;

	/** The form that allows the user to add/edit a Project */
	private ProjectForm projectForm;

	/**Command for selecting a list*/
	private final Command cmdSelect = new Command("Select");

	/**Command for adding a new list*/
	private final Command cmdAdd = new Command("Add");

	/**Command to edit a list*/
	private final Command cmdEdit = new Command("Edit");

	/**Command to issue a delete*/
	private final Command cmdDelete = new Command("Delete");

	/**Command to start logging item*/
	private final Command cmdLog = new Command("Log time");

	/**The last issued command (either Add or Edit)*/
	private Command lastCommand;

	/**The form used to log time*/
	private LogTimeForm logTimeForm;

	/**
	 * Creates a new ProjectList from the given list of Projects
	 * @param projects
	 */
	public ProjectListUI(Vector projects) {
		super.addComponent(this.lstProjects = new List(projects));
		super.addCommand(cmdAdd);
		super.addCommand(cmdEdit);
		super.addCommand(cmdDelete);
		super.addCommand(cmdLog);

		this.lstProjects.addActionListener(this);
		super.addCommandListener(this);

		// Add effects
		this.setTransitionOutAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, false, 200));
		this.setTransitionInAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 200));
	}

	/**
	 * Get a copy of all Projects stored as a vector
	 * 
	 * @return
	 */
	public Vector getProjects() {
		Vector projects = new Vector();
		for (int i = 0; i < this.lstProjects.getModel().getSize(); i++) {
			projects.addElement(this.lstProjects.getModel().getItemAt(i));
		}
		return projects;
	}

	/**
	 * Creates or retrieves a task list form
	 */
	private Form getProjectForm(Project project) {
		// Lazy initialization
		if (this.projectForm == null) {
			this.projectForm = new ProjectForm();
			this.projectForm.addCommandListener(this);
		}
		this.projectForm.setProject(project);
		return this.projectForm;
	}

	public void actionPerformed(ActionEvent ae) {
		System.out.println(ae.getSource().getClass().toString());
		System.out.println(ae.getSource());
		if (ae.getSource() == cmdAdd) {
			this.lastCommand = cmdAdd;
			getProjectForm(null).show();
		} else if (ae.getSource() == cmdEdit) {
			this.lastCommand = cmdEdit;
			getProjectForm((Project) lstProjects.getSelectedItem()).show();
		} else if (projectForm != null && ae.getSource() == projectForm.cmdOk) {
			Project project = projectForm.getProject();
			if (this.lastCommand == cmdAdd)
				lstProjects.addItem(project);
			lstProjects.repaint();
			this.show();
		} else if (projectForm != null && ae.getSource() == projectForm.cmdCancel) {
			this.show();
		} else if (ae.getSource() == cmdDelete) {
			if (Dialog.show("Confirmation", "Are you sure you want to delete?", "OK", "Cancel")) {
				Project deletedProject = (Project) lstProjects.getSelectedItem();
				lstProjects.getModel().removeItem(lstProjects.getSelectedIndex());
				deletedProject.removeTimeEntries();
			}
		} else if (ae.getSource() == cmdLog) {
			this.logTimeForm = getLogTimeForm();
			this.logTimeForm.show();
		} else if (ae.getSource() == lstProjects){
			// Select command for the list
			// TODO don't create new form. Use one form all the time
			TaskListUI taskListUI = new TaskListUI((Project) lstProjects.getSelectedItem(), this);
			taskListUI.show();
		} else if (ae.getSource() == this) {
			// key listener
			if (ae.getKeyEvent() == Canvas.KEY_NUM6) {
				
			}
		}
	}

	private LogTimeForm getLogTimeForm() {
		if (logTimeForm == null) {
			logTimeForm = new LogTimeForm();
			logTimeForm.setProjects(lstProjects.getModel());
			logTimeForm.addCommandListener(this);
		}
		return logTimeForm;
	}
}
